package com.example.shop_2304.xq

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.widget.Button
import android.widget.EditText
import android.widget.PopupWindow
import android.widget.RatingBar
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.ActionBar.LayoutParams
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop_2304.R
import com.example.shop_2304.adapter.PlAdapter
import com.example.shop_2304.databinding.ActivityXqDetailBinding
import com.example.shop_2304.room.AppDbUtils
import com.example.shop_2304.room.Pinglun

@Route(path = "/app/XqDetailActivity")
class XqDetailActivity : BaseActivity<ActivityXqDetailBinding,XqViewModel>() {
    @Autowired
    @JvmField
    var gid = 1

    var attrId = ""//选中对应商品的属性id

    lateinit var plAdapter: PlAdapter
    override fun initView() {
        ARouter.getInstance().inject(this)
        viewModel.xq(gid,"normal")

        binding.tvAddCar.setOnClickListener {
            viewModel.addCar(1,attrId,gid)
        }
        plAdapter = PlAdapter()
        binding.rvComment.adapter = plAdapter
        binding.rvComment.layoutManager = LinearLayoutManager(this)
        queryPl()

        //发评论
        binding.tvCommentCount.setOnClickListener {
            var popupWindow = PopupWindow(this)
            popupWindow.width = LayoutParams.MATCH_PARENT
            popupWindow.height = 300
            val view = LayoutInflater.from(this).inflate(R.layout.pl_pop, null)
            view.findViewById<Button>(R.id.btn).setOnClickListener {
                //判断有没有值  评分给没给
                val et = view.findViewById<EditText>(R.id.et).text.toString()
                val rating = view.findViewById<RatingBar>(R.id.rb).rating
                if(et.isEmpty()||rating==0f){
                    ToastUtils.showLong("必须输入内容和选择评分")
                }else{
                    AppDbUtils.getDb().getPlDao().insert(Pinglun(0,rating.toInt(),et,"张三",0,"2025-8-5",gid))

                    view.findViewById<EditText>(R.id.et).setText("")
                    view.findViewById<RatingBar>(R.id.rb).rating = 0f
                    queryPl()
                    popupWindow.dismiss()
                }
            }
            popupWindow.contentView = view
            popupWindow.isOutsideTouchable = true
            popupWindow.isFocusable = true
            popupWindow.showAtLocation(binding.tvCommentCount,Gravity.BOTTOM,0,0)
        }
    }

    private fun queryPl() {
        val query = AppDbUtils.getDb().getPlDao().query(gid)
        query.reverse()
        plAdapter.data.clear()
        plAdapter.data.addAll(query)
        plAdapter.notifyDataSetChanged()
    }

    override fun initData() {
        viewModel.xqSuccess.observe(this){
            if(it.code==200){
                binding.info = it.data.productInfo
                //属性没赋值
                attrId = it.data.productValue.values.toList()[0].id.toString()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.addSuccess.observe(this){
            ToastUtils.showLong(it.message)
            //设置动画效果
        }
    }

    override fun getViewModel(): Class<XqViewModel> = XqViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_xq_detail

}